/**
 * 
 */
package com.cuc.platform.core.componet.security.service.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cuc.platform.core.componet.security.SecurityFacadeService;
import com.cuc.platform.core.componet.security.dao.SecurityAuthorityDao;
import com.cuc.platform.core.componet.security.dao.SecurityMenuDao;
import com.cuc.platform.core.componet.security.dao.SecurityUserDao;
import com.cuc.platform.core.componet.security.domain.SecurityAuthority;
import com.cuc.platform.core.componet.security.domain.SecurityMenu;
import com.cuc.platform.core.componet.security.domain.SecurityUser;
import com.cuc.platform.core.componet.security.service.SecurityUserService;
import com.cuc.platform.core.service.ServiceException;

/**
 * @author peepul
 *
 * 权限验证相关Facade的业务实现类
 */
@Service("SecurityFacadeService")
public class SecurityFacadeServiceImpl implements SecurityFacadeService {
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityFacadeService#getUserByLoginName(java.lang.String)
	 */
	@Override
	public SecurityUser getUserByLoginName(String loginName) {
		return securityUserDao.getUserByLoginName(loginName);
	}	

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityFacadeService#getUserWithRoles(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getUserWithRoles(String userName) {
		if((null == userName)||(userName.equals(""))) return null;
		
		return securityUserDao.getUserWithRoles(userName);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityFacadeService#getAuthsByRoleId(java.lang.String)
	 */
	@Override
	public List<SecurityAuthority> getAuthsByRoleId(Long roleId) {
		if(null == roleId) return null;
		
		return securityAuthorityDao.getAuthsByRoleId(roleId);
	}
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityFacadeService#updatePassword(java.lang.String, java.lang.String)
	 */
	@Override
	public void updatePassword(SecurityUser user) throws ServiceException {
		user = securityUserService.editEntryptPassword(user);
		securityUserDao.updateUser(user);
	}
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityFacadeService#refreshAuthResc()
	 */
	@Override
	public void refreshAuthResc() {
//		resourceDetailsMonitor.refresh();
	}
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityFacadeService#listMenuByUser(java.lang.String)
	 */
	@Override
	public List<SecurityMenu> listMenuByUser(String userName) {
		if((null == userName)||(userName.equals(""))) return null;
		
		return securityMenuDao.listMenuByUser(userName);
	}
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityFacadeService#listMenuByUserWithLevel(java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<SecurityMenu> listMenuByUserWithLevel(String userName, Integer level) {
		//校验
		if((null == userName)||(userName.equals(""))) return null;
		if (null == level) {
			logger.warn("listMenuByUserWithLevel()：没有设置菜单级别level，系统默认设置为1级菜单。");
			level = 1;
		}
		
		//构造查询参数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userName", userName);
		paramMap.put("level", level);
		
		return securityMenuDao.listMenuByUserWithLevel(paramMap);
	}
	
	private static Logger logger = Logger.getLogger(SecurityFacadeServiceImpl.class);
	
	/**
	 * Ioc beans
	 */
	@Resource
	private SecurityUserService securityUserService;
	@Resource
	private SecurityUserDao securityUserDao;
	@Resource
	private SecurityAuthorityDao securityAuthorityDao;
	@Resource
	private SecurityMenuDao securityMenuDao;
	
    
}
